package putfile;


import java.io.File;  
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import ch.ethz.ssh2.Connection;  
import ch.ethz.ssh2.SCPClient;

public class TestPut {
	
	

	  

	    private static String IP = "192.168.0.245";  
	    private static int PORT = 22;  
	    private static String USER = "root";//  远程Linux服务器的用户名 
	    private static String PASSWORD = "root";// 远程Linux服务器的登录密码
	    private static Connection connection = new Connection(IP, PORT);  
	    private static boolean usePassword = true;// 使用用户名和密码来进行登录验证
	  
	    /** 
	     * ssh用户登录验证，使用用户名和密码来认证 
	     *  
	     * @param user 
	     * @param password 
	     * @return 
	     */  
	    
	    
	  
	    public static void app() {  
	    	try {   
	    		putFile("e://config.xml", "/home");  
	    	} catch (Exception e) {  
	    		e.printStackTrace();  
	    	}  
	    }  
	    
	    public static void putFile(String localFile, String remoteTargetDirectory) {  
	    	try {  
	    		connection.connect();  
	    		boolean isAuthed = isAuth();  
	    		if (isAuthed) {  
	    			SCPClient scpClient = connection.createSCPClient();  
	    			scpClient.put(localFile, remoteTargetDirectory);  
	    			System.out.println("传输成功！");
	    		} else {  
	    			System.out.println("传输失败!");  
	    		}  
	    	} catch (Exception ex) {  
	    		ex.printStackTrace();  
	    	} finally {  
	    		connection.close();  
	    	}  
	    }  
	    
	    public static boolean isAuth() {  
	    	if (usePassword) {  
	    		return isAuthedWithPassword(USER, PASSWORD);  
	    	}
	    	return false;
	    }  
	    
	    
	    public static boolean isAuthedWithPassword(String user, String password) {  
	        try {  
	            return connection.authenticateWithPassword(user, password);  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	        return false;  
	    }  
	  
	 
	  public static void main(String[] args) {
	  
		  File file=new File("D:\\FTP\\config.csv");
          Date date=new Date();
          SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMdd");
          String time = sdf.format(date);
          String name="config_"+time+".csv";
          File newfile=new File("D:\\FTP"+"\\"+name);
          
          if(file.exists()){
             if(file.renameTo(newfile)){
             System.out.println("重命名成功");
             }else{
             System.out.println("重命名失败");
             }
          }  
	}
	  
	

}
